using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Linq;

using LDAP.Entity;

namespace LDAP.BLL
{
    public class BizExceptionform
    {
		public const string ENTITY_KEY = "exceptionform";
		
        public static long Add(Exceptionform entry)
        {
            Common.GetMapper(ENTITY_KEY, entry).Insert("Insert_Exceptionform", entry);
            BizAttachment.UpdateListStatus(entry.Attachement, entry.Id);
            return entry.Id;
        }

        public static int Update(Exceptionform entry)
        {
            return Common.GetMapper(ENTITY_KEY, entry).Update("Update_Exceptionform", entry);
        }

        public static Exceptionform GetByKey(long id)
        {
            return Common.GetMapper(ENTITY_KEY, id).QueryForObject<Exceptionform>("Select_Exceptionform", id);
        }

        public static List<Exceptionform> GetByUserId(string userId, DateTime start, DateTime end)
        {
            Hashtable ht = new Hashtable();
            ht.Add("userId", userId);
            ht.Add("start", start);
            ht.Add("end", end);
            return Common.GetMapper(ENTITY_KEY, ht).QueryForList<Exceptionform>("Select_Exceptionform_User", ht).ToList();
        }

        public static List<Hashtable> GetByUserList(string userList, DateTime start, DateTime end)
        {
            Hashtable ht = new Hashtable();
            ht.Add("userList", userList);
            ht.Add("start", start);
            ht.Add("end", end);
            ht.Add("status", 100);
            return Common.GetMapper(ENTITY_KEY, ht).QueryForList<Hashtable>("Select_Exceptionform_UserList", ht).ToList();
        }

        public static List<Exceptionform> GetServiceListByUserId(User user, DateTime start, DateTime end)
        {
            Angel angel = BizAngel.GetByKey(user.Id);
            if (angel == null)
                return null;

            string[] deptArray = angel.Dept.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < deptArray.Length; i++)
            {
                sb.Append(" dept like '%;" + deptArray[i] + ";%' ");
                if (i < deptArray.Length - 1)
                    sb.Append(" or ");
            }

            Hashtable ht = new Hashtable();            
            ht.Add("dept",sb.ToString());
            ht.Add("start", start);
            ht.Add("end", end);
            return Common.GetMapper(ENTITY_KEY, ht).QueryForList<Exceptionform>("Select_Exceptionform_UserService", ht).ToList();
        }

        public static int Delete(int id)
        {
           return Common.GetMapper(ENTITY_KEY, id).Delete("Delete_Exceptionform",id);
        }
    }
}

	